package com.foreknow.dao.impl;

import com.foreknow.bean.Business;
import com.foreknow.dao.BusinessDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class BusinessDaoImpl extends BaseDao implements BusinessDao {
	
	private Connection con = null;
	private PreparedStatement pst = null;
	private ResultSet rs = null;

	@Override
	public List<Business> listBusiness(String businessName, String businessAddress) {
		List<Business> listBusiness = new ArrayList<>();
		StringBuffer sql = new StringBuffer("select * from business where 1=1 ");
		if(businessName!=null&&!businessName.equals("")) {
			sql.append(" and businessName like '%"+businessName+"%' ");
		}
		if(businessAddress!=null&&!businessAddress.equals("")) {
			sql.append(" and businessAddress like '%"+businessAddress+"%' ");
		}
		EntityMapping mapping = mappingFactory.mapping(MappingFactory.BUSINESS_MAPPING);
		List<Object> list = jdbcTemplate.query(sql.toString(),mapping);
		for(int i = 0;i<list.size();i++){
			Business business = (Business) list.get(i);
			listBusiness.add(business);
		}
		return listBusiness;
	}
	
	@Override
	public int saveBusiness(String businessName) {
		int businessId = 0;
		String sql = "insert into business(businessName,password) values(?,'123')";

		return businessId;
	}
	
	@Override
	public int removeBusiness(int businessId) {
		int result = 0;
		String delFootSql = "delete from food where businessId=?";
		String delBusinessSql = "delete from business where businessId=?";

		return result;
	}
	
	@Override
	public Business getBusinessByIdByPass(Integer businessId, String password) {
		Business business = null;
		String sql = "select * from business where businessId=? and password=?";
		return business;
	}
	
	@Override
	public Business getBusinessById(Integer businessId) {
		Business business = null;
		String sql = "select * from business where businessId=?";
		return business;
	}
	
	@Override
	public int updateBusiness(Business business) {
		int result = 0;
		String sql = "update business set businessName=?,businessAddress=?,businessExplain=?,starPrice=?,deliveryPrice=? where businessId=?";

		return result;
	}
	
	@Override
	public int updateBusinessByPassword(Integer businessId,String password) {
		int result = 0;
		String sql = "update business set password=? where businessId=?";
		return result;
	}
}
